layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 系统管理--消息管理
     */
    var Notice = {
        tableId: "noticeTable"    //表格id
    };
    $("#condition").on("input",function(e){
        //获取input输入的值
        Notice.search();
    });

    $("#ghs").on("input",function(e){
        //获取input输入的值
        Notice.search();
    });
    /**
     * 选中行效果
     */
    table.on('row(noticeTable)',function(obj){
        $(obj.tr).siblings().css("background-color","");
        obj.tr.css({"background-color":"#dadad9"});
    });
    /**
     * 检索店名
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyBxda/getDm");
    var languageResult = languageAjax.start();
    console.info(languageResult);
    $.each(languageResult, function( index, val ) {
        $("#dm").append("<option value='"+val.dm+"'>"+val.dm+"</option>");
    } );
    form.render('select');
    /**
     * 初始化表格的列
     */
    Notice.initColumn = function () {
        return [[
            {type: 'checkbox',hide: true,},
            {field: 'id', align: "center", hide: true, sort: true, title: 'id'},
            // {align: 'center', type: "numbers", title: '序号'},
            // {field: 'bh', align: "center", sort: true, title: '编号',width:180},
            {field: 'dm', sort: true, title: '店名', width: 160, fixed: 'left'},
            {field: 'xm', title: '姓名', fixed: 'left', width: 120},
            {field: 'ghs', title: '供货商名称', width:120},
            // {field: 'sfz', align: "center", sort: true, title: '身份证号',width:180},
            {field: 'yhk', sort: true, title: '银行卡号', width:200},
            {field: 'bank', title: '银行简称', width: 90},
            {field: 'bz', sort: true, title: '行号', width: 130},
            //{field: 'bank', align: "center", sort: true, title: '开户行'},
            //{field: 'balance', align: "center", sort: true, title: '余额'},
            {field: 'kj', sort: true, title: '会计', width: 130},
            //{field: 'bz', align: "center", sort: true, title: '备注'},
            {toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Notice.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['ghs'] = $("#ghs").val();
        queryData['status'] = $("#status").val();
        queryData['dm'] = $("#dm").val();
        table.reload(Notice.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加通知
     */
    Notice.openAddNotice = function () {
        func.open({
            height: 600,
            title: '添加报销档案',
            content: Feng.ctxPath + '/btyBxda/btyBxda_add',
            tableId: Notice.tableId
        });
    };
    /**
     * 点击删除通知
     *
     * @param data 点击按钮时候的行数据
     */
    Notice.onDeleteNotice = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyBxda/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Notice.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除报销档案 ?", operation);
    };
    /**
     * 点击修改档案
     *
     * @param data 点击按钮时候的行数据
     */
    Notice.onEditNotice = function (data) {
        func.open({
            height: 600,
            title: '修改报销档案',
            content: Feng.ctxPath + "/btyBxda/btyBxda_update/" + data.id,
            tableId: Notice.tableId
        });
    };

    /**
     * 点击批量修改会计
     */
    Notice.onEditKj = function (data) {
        func.open({
            height: 600,
            title: '修改会计',
            content: Feng.ctxPath + "/btyBxda/btyBxda_updateKj/" + data.id,
            tableId: Notice.tableId
        });
    };


    /**
     * 点击识别行号
     */
    Notice.btnSbhhNotice = function (data) {
        $.ajax({
            type: "PUT",
            url: "/btyBxda/updateBz",
            datatype: "json",
            data: data,
            success: function (data) {
                table.reload(Notice.tableId);
                Feng.success("识别完成！")
            }, error: function () {
                Feng.error("修改失败！");
            }
        });
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Notice.tableId,
        url: Feng.ctxPath + '/btyBxda/list',
        page: true,
        cellMinWidth: 60,
        limit: 20,
        height: "full-59",
        skin: 'line',
        cols: Notice.initColumn(),
        // done:function (res, curr, count) {
        //     //图片预览
        //     layer.photos({
        //         photos: '.show_img',
        //         anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
        //     });
        // }
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Notice.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Notice.openAddNotice();
    });

    // 识别行号按钮点击事件
    $('#btnSbhh').click(function () {
        Notice.btnSbhhNotice();
    });


    // 工具条点击事件
    table.on('tool(' + Notice.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Notice.onEditNotice(data);
        } else if (layEvent === 'delete') {
            Notice.onDeleteNotice(data);
        }else if (layEvent === 'editKj') {
            Notice.onEditKj(data);
        }
    });
});
